package cn.xorange.commons.system.service.System.impl;

import cn.xorange.commons.core.basic.service.BaseService;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.system.SysPost;
import cn.xorange.commons.system.entity.system.SysUserPost;
import cn.xorange.commons.system.mapper.system.SysRoleManageMapper;
import cn.xorange.commons.system.mapper.system.SysUserPostMapper;
import cn.xorange.commons.system.mapper.system.SysPostMapper;
import cn.xorange.commons.system.service.System.ISysPostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : yangjian
 * date : 2025/1/9
 * description :
 */
@Service
public class SysPostServiceImpl extends BaseService<SysPost, SysPostMapper> implements ISysPostService {

    @Resource
    SysRoleManageMapper roleManageMapper;
    @Resource
    SysUserPostMapper userPostMapper;

    @Override
    public List<SysPost> getList(SysPost t){
        SecurityUser user = SecurityUtils.getUser();
        t.setTenantId(user.getTenantId());
        return mapper.selectManageList(user.getIsAdmin(),user.getUserId(), t);
    }

    @Override
    @Transactional
    public int deleteById(Long id){
        // 删除管理角色管理的岗位Id
        roleManageMapper.delRoleManagePostId(id);
        // 删除用户的管理Id
        SysUserPost userPost = new SysUserPost();
        userPost.setPostId(id);
        userPostMapper.del(userPost);
        return mapper.delById(id, SysPost.class);
    }


    @Override
    public String getRoleIdsByPostId(Long[] ids) {
        return mapper.selectRoleIdsByPostId(ids);
    }

    @Override
    public String getPostNamesByUserId(Long userId) {
        return mapper.selectPostNames(userId);
    }
}
